package com.ldb.Controller;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.portlet.ModelAndView;

import com.ldb.Controller.entity.Committee;
import com.ldb.Controller.entity.Student;
import com.ldb.Controller.entity.User;
import com.ldb.Controller.service.UserManager;
import com.ldb.Controller.service.CommitteeManager;
import com.ldb.Controller.service.StudentManager;

@Controller
@RequestMapping("/software")
public class UserController {
	private final static String DESCRIPTION = "这个家伙很懒，什么也不想说";
	private final static String DESCRIPTION_COMMITTEE = "干部在忙，介绍稍后来补上，请不要介意哈！";
	private final static String ImageUrl = "../images/student/";
	private final static String ImageUrl_committee = "../images/committee/";

	@Resource(name="UserManager")
	public UserManager UserManager;
	
	@RequestMapping("/addUser")
	public String addUser(User user)
	{
		UserManager.addUser(user);
		return "/success"; 
	}
	@RequestMapping("/toaddUser")
	public String toaddUser()
	{
		return "/addUser";
	}
	
	@Resource(name="StudentManager") 
	public StudentManager studentManager;
	
	@RequestMapping("/toAminLogin")
	public String toAdminLogin()
	{
		return "/adminLogin";
	}
	
	@RequestMapping("/toAddStudent")
	public String toAddStudent() {
		return "addStudent";
	}
	@RequestMapping(value ="/addImage")
	public String addImage(@RequestParam(value ="file") MultipartFile file, HttpServletRequest request, ModelMap model,String type) {  
       // String path = request.getSession().getServletContext().getRealPath("/images/student");
        String  path = HomeController.ImagePathBase+"/"+type;
        String fileName = file.getOriginalFilename();  
        File targetFile = new File(path, fileName);  
        //保存  
        try {  
            file.transferTo(targetFile);  
            System.out.println(path);
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
        model.addAttribute("fileUrl", "../images/"+type+"/"+fileName);  
        return "save"+type+"image";
    }  
  
	
	@RequestMapping("/addStudent")
	/*
	 * 新增学生
	 */
	public String addStudent(Student student,Map<String,String> msgs)
	{
		
		String msg = null;
		if(student.getName() == null || student.getName() == "") {
			msg = "姓名不能为空";
			msgs.put("messageErr", msg);
			return "/addStudent";
		}
		if(student.getDescription() == null || student.getDescription() == "") {
			student.setDescription(DESCRIPTION);
		}
		/*if(student.getImageUrl() != null && !student.getImageUrl() .equals("")) {
			student.setImageUrl(ImageUrl+student.getImageUrl());
		}else {
			student.setImageUrl(ImageUrl+"defualt.jpg");
		}*/	
		studentManager.addStudent(student);
		msgs.put("messageSucc", "学生成员添加成功！");
		return "addStudent";
	}
	@RequestMapping("/showStudent")
	/*
	 * 前台展示所有学生成员
	 */
	public String showStudent(Map<String,Object> map) {
		List<Student> studentList = this.studentManager.getAllStudent();
		map.put("studentLists", studentList);
		return "showStudent";
	}
	
	@RequestMapping(value="/admin_allStudent")
	/*
	 * 后台显示所有学生  按姓名查找学生
	 */
	public String adminShowAllStudent(String name,Map<String,Object> map) {
		if(name!=null && name =="") {
			map.put("errMessage", "输入为空！");
			return "admin_allStudents";
		}else if(name != null && name != "") {
			List<Student> studentList = this.studentManager.getStudentByName("name", name);
			map.put("studentLists", studentList);
			return "admin_allStudents";
		}
		List<Student> studentList = this.studentManager.getAllStudent();
		map.put("studentLists", studentList);
		return "admin_allStudents";
	}
	
	@RequestMapping("/editStudent")
	/*
	 * 编辑学生信息
	 */
	public String editStudent(String student_id,Map<String,Object> map) {
		Student student = this.studentManager.getStudentById(student_id);
		map.put("student", student);
		return "editStudent";
	}
	
	@RequestMapping("/updateStudent")
	/*
	 * 更新学生信息
	 */
	public String updateStudent(Student student,Map<String,Object> map) {
		if(student.getName() == null || student.getName() == "") {
			map.put("messageErr", "姓名不能为空");
			return "/editStudent";
		}
		if(student.getDescription() == null || student.getDescription() == "") {
			student.setDescription(DESCRIPTION);
		}
		if(student.getImageUrl() != null && !student.getImageUrl() .equals("")) {
			student.setImageUrl(student.getImageUrl());
		}else {
			student.setImageUrl(ImageUrl+"defualt.jpg");
		}
		boolean result = this.studentManager.updateStudent(student.getStudent_id(), student);
		if(result) {
			map.put("messageSuc", "操作成功！");
		}else {
			map.put("messageErr", "操作失败，请重试！");
		}
		return "editStudent";
	}
	
	@RequestMapping("/deleteStudent")
	public String deleteStudent(String student_id,Map<String,Object> map) {
		this.studentManager.deleteStudent(student_id);
		return "redirect:/software/admin_allStudent";
	}
	
	@Resource(name="committeeManager")
	private CommitteeManager committeeManager;
	
	@RequestMapping("/toAddCommittee")
	public String toAddCommittee() {
		return "addCommittee";
	}
	
	@RequestMapping("/addCommittee")
	/*
	 * 增加班干
	 */
	public String addCommittee(Committee committee,Map<String,String> msgs) {
		String msg = null;
		if(committee.getName() == null || committee.getName() == "") {
			msg = "姓名不能为空";
			msgs.put("messageErr", msg);
			return "/addCommittee";
		}
		if(committee.getDuty() == null || committee.getDuty() == "") {
			msg = "职务不能为空";
			msgs.put("messageErr", msg);
			return "/addCommittee";
		}
		if(committee.getDescription() == null || committee.getDescription() == "") {
			committee.setDescription(DESCRIPTION_COMMITTEE);
		}
		if(committee.getImageUrl() != null && !committee.getImageUrl().equals("")) {
			committee.setImageUrl(committee.getImageUrl());
		}else {
			committee.setImageUrl(ImageUrl_committee+"defualt.jpg");
		}
		this.committeeManager.addCommitte(committee);
		msgs.put("messageSucc", "班委添加成功！");
		return "/addCommittee";
	}
	
	@RequestMapping("/deleteCommittee")
	/*
	 * 删除班干
	 */
	public String deleteCommittee(String committee_id) {
		this.committeeManager.deleteCommittee(committee_id);
		return "redirect:/software/admin_allCommittee";
	}
	
	@RequestMapping("/editCommittee")
	/*
	 * 编辑班干信息
	 */
	public String editCommittee(String committee_id,Map<String,Object> map) {
		Committee committee = this.committeeManager.getCommitteeById(committee_id);
		map.put("committee",committee);
		return "/editCommittee";
	}
	
	@RequestMapping("/updateCommittee")
	public String updateCommittee(Committee committee,Map<String,String> map) {
		if(committee.getName() == null || committee.getName() == "") {
			map.put("messageErr", "姓名不能为空");
			return "/editCommittee";
		}
		if(committee.getDuty() == null || committee.getDuty() == "") {
			map.put("messageErr", "职务不能为空");
			return "/editCommittee";
		}
		if(committee.getDescription() == null || committee.getDescription() == "") {
			committee.setDescription(DESCRIPTION_COMMITTEE);
		}
		if(committee.getImageUrl() != null && !committee.getImageUrl().equals("")) {
			committee.setImageUrl(committee.getImageUrl());
		}else {
			committee.setImageUrl(ImageUrl_committee+"defualt.jpg");
		}
		this.committeeManager.updateCommittee(committee);
		return "editCommittee";
	}
	
	@RequestMapping("/showCommittees")
	/*
	 * 前台展示所有班干信息
	 */
	public String showCommittees(Map<String,Object> map) {
		List<Committee> committeeList = this.committeeManager.getAllCommittees();
		map.put("studentLists", committeeList);
		return "showCommittees";
	}
	
	@RequestMapping("/admin_allCommittee")
	public String adminAllCommittee(Map<String,Object> map) {
		List<Committee> committeeList = this.committeeManager.getAllCommittees();
		map.put("committeeList", committeeList);
		return "/admin_allCommittee";
	}
}
